﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using Kang.ExtendMethod;
using Kang.ExtendMethod.Log;
using Kang.Test.WPF.SqlFormatter;
using Kang.Test.WPF.SqlFormatter.Core;
using Kang.Test.WPF.SqlFormatter.Languages;
using Vanara.PInvoke;

namespace Kang.Test.WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        readonly Logger logger = LogManager.GetCurrentClassLogger();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //CredUI.CREDUI_INFO info = new CredUI.CREDUI_INFO();
            //info.pszCaptionText = "登录大伽";
            //info.pszMessageText = "欢迎使用大伽智能病理系统";
            //info.cbSize = Marshal.SizeOf(info);
            //uint pulAuthPackage = 0, ulInAuthBufferSize = 0, pulOutAuthBufferSize;
            //IntPtr pvInAuthBuffer = IntPtr.Zero, ppvOutAuthBuffer = IntPtr.Zero, pAuthBuffer = IntPtr.Zero;
            //bool isSave = false;
            //int maxUnSize = 50, maxPwSize = 50, maxDomSize = 50;
            //StringBuilder un = new StringBuilder(maxUnSize), pw = new StringBuilder(maxPwSize), dom = new StringBuilder(maxDomSize);

            //var error = CredUI.CredUIPromptForWindowsCredentials(info, Win32Error.NO_ERROR, ref pulAuthPackage, pvInAuthBuffer, ulInAuthBufferSize, out ppvOutAuthBuffer, out pulOutAuthBufferSize, ref isSave, CredUI.WindowsCredentialsDialogOptions.CREDUIWIN_GENERIC);
            //if (error == Win32Error.ERROR_CANCELLED)
            //{
            //    logger.Debug($"{error}");
            //    return;
            //}
            //if (error != Win32Error.ERROR_SUCCESS)
            //{
            //    logger.Debug($"{error}");
            //    return;
            //}
            //var flag = CredUI.CredUnPackAuthenticationBuffer(CredUI.CredPackFlags.CRED_PACK_GENERIC_CREDENTIALS, ppvOutAuthBuffer, (int)pulOutAuthBufferSize, un, ref maxUnSize, dom, ref maxDomSize, pw, ref maxPwSize);
            //if (un.ToString().IsBlank())
            //{
            //    logger.Debug($"用户名不能为空！");
            //    return;
            //}
            //if (pw.ToString().IsBlank())
            //{
            //    logger.Debug($"密码不能为空！");
            //    return;
            //}
            //if (!flag)
            //{
            //    logger.Debug($"获取用户名及密码信息失败！");
            //    return;
            //}
            //logger.Debug($"用户名：{un}    密码：{pw}");


            //CredUI.CREDUI_INFO info = new CredUI.CREDUI_INFO();
            //info.pszCaptionText = "登录大伽";
            //info.pszMessageText = "欢迎使用大伽智能病理系统";
            //info.cbSize = Marshal.SizeOf(info);
            //IntPtr Reserved = IntPtr.Zero;
            //bool isSave = false;
            //int maxUnSize = 50, maxPwSize = 50;
            //StringBuilder un = new StringBuilder(maxUnSize), pw = new StringBuilder(maxPwSize);

            //var error = CredUI.CredUIPromptForCredentials(info, "www.dakapath.com/dakapath", Reserved, Win32Error.NO_ERROR, un, 10, pw, 10, ref isSave, CredUI.CredentialsDialogOptions.CREDUI_FLAGS_DEFAULT);
            //logger.Error($"{error}");
            Formatter formatter = new StandardSqlFormatter();
            txt_sql.Text = formatter.Format(txt_sql.Text, new SqlFormatterOptions(
                            SqlFormatter.Models.Indentation.TwoSpaces,
                            uppercase: true,
                            linesBetweenQueries: 2));

        }
    }
}
